package com.xiaochun.hxhj.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.util.InfoEventMessage;
import com.xiaochun.hxhj.R;
import com.xiaochun.hxhj.WebviewActivity;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID,false);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		Log.e("支付", "onPayFinish, errCode = " + req.openId);
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.e("支付", "onPayFinish, errCode = " + resp.errCode+resp.errStr+"xxx");

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("提示");
			builder.setMessage("支付结果"+ String.valueOf(resp.errCode));
			
			Log.e("----------》》》》", String.valueOf(resp.errCode)) ;
//			builder.show();
			if( String.valueOf(resp.errCode).equals("-1")){

				if(WebviewActivity.class != null){
					EventBus.getDefault().post(new InfoEventMessage("微信支付失败"));
				}
				/*if(WebviewActivity.class != null){
					EventBus.getDefault().post(new InfoEventMessage("微信支付失败"));
				}*/
			}else if( String.valueOf(resp.errCode).equals("-2")){

				if(WebviewActivity.class != null){
					EventBus.getDefault().post(new InfoEventMessage("用户取消支付"));
				}
				/*if(WebviewActivity.class != null){
					EventBus.getDefault().post(new InfoEventMessage("用户取消支付"));
				}*/

			}else {
				if(WebviewActivity.class != null){
					EventBus.getDefault().post(new InfoEventMessage("微信支付成功"));
				}
//				if(WebviewActivity.class != null){
//					EventBus.getDefault().post(new InfoEventMessage("微信支付成功"));
//				}
			}

 			finish();
		}
		if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
			Toast.makeText(this, "code = " + ((SendAuth.Resp) resp).code, Toast.LENGTH_SHORT).show();
		}
		
		int result = 0;
		
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = R.string.errcode_success;
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = R.string.errcode_cancel;
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = R.string.errcode_deny;
			break;
		default:
			result = R.string.errcode_unknown;
			break;
		}

	}
}